package com.travel.api.controller.message

import com.lgy.sell.controller.BaseController
import com.travel.api.beans.request.message.EmailRequest
import com.travel.api.beans.request.message.SMSRequest
import com.travel.api.utils.ResultConstant
import com.travel.api.service.message.MessageService
import com.travel.api.utils.CommonUtil
import com.travel.api.utils.RedisUtil
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.*

@Api(value = "message", description = "消息")
@RestController
@RequestMapping("message")
@CrossOrigin
class MessageController : BaseController() {

    @Value("\${SMS_NUMBER_PREFIX}")
    private val SMS_NUMBER_PREFIX: String? = null

    @Value("\${SMS_VALIDATION_PREFIX}")
    private val SMS_VALIDATION_PREFIX: String? = null

    @Value("\${EMAIL_NUMBER_PREFIX}")
    private val EMAIL_NUMBER_PREFIX: String? = null

    @Autowired
    private val messageService: MessageService? = null

    /**
     * 短信发送
     * @return
     */
    @ApiOperation(value = "短信发送", httpMethod = "GET", notes = "短信发送")
    @GetMapping("/sendSMS")
    fun sendSMS(@RequestParam("mobile") mobile: String): Map<String, Any> {
        // 先判断请求次数是否超过5次
        val redisNumber = RedisUtil.get(SMS_NUMBER_PREFIX!! + mobile)
        var number: Int? = 0
        if (!StringUtils.isEmpty(redisNumber)) {
            number = Integer.parseInt(redisNumber!!)
            if (number > 5) {
                return fail(ResultConstant.SMS_TOO_MANY, "今天的短信请求次数达到上限")
            }
        }
        // 再判断该手机号在1分钟内是否已经发送验证码
        val redisCode = RedisUtil.get(SMS_VALIDATION_PREFIX!! + mobile)
        if (!StringUtils.isEmpty(redisCode)) {
            return fail(ResultConstant.SMS_ALREADY, "短信验证码1分钟内只能发送一次")
        }

        val code = CommonUtil.randomNo(4).toString() // 验证码
        // 次数和验证码存入Redis中 次数Key存活时间为1天 验证码Key上限为1分钟
        RedisUtil.setValue(SMS_NUMBER_PREFIX + mobile, (number!! + 1).toString(), 60 * 60 * 24)
        RedisUtil.setValue(SMS_VALIDATION_PREFIX + mobile, code, 60)
        // 发送验证码
        // messageService!!.sendSMS(mobile, code) // TODO 接口已经调通需要调用的打开注释就行, 测试暂时不调用发送接口次数伤不起
        log.info("[短信发送]手机号[{}], 验证码[{}]", mobile, code)
        return success()
    }

    /**
     * 短信验证码验证
     * @param request
     * @return
     */
    @ApiOperation(value = "短信验证码验证", httpMethod = "POST", notes = "短信验证码验证")
    @PostMapping("/validationSMS")
    fun validationSMS(@RequestBody request: SMSRequest): Map<String, Any> {
        return messageService!!.validationSMS(request)
    }

    /**
     * 邮箱发送
     * @return
     */
    @ApiOperation(value = "邮箱发送", httpMethod = "GET", notes = "邮箱发送")
    @GetMapping("/sendEmail")
    fun sendEmail(@RequestParam("email") email: String): Map<String, Any> {
        val code = CommonUtil.randomNo(4).toString() // 验证码
        // 这里只将验证码存入REDIS中即可, 设置有效时间为1天
        RedisUtil.setValue(EMAIL_NUMBER_PREFIX!! + email, code, 60 * 60 * 24)
        messageService!!.sendEmail(email, code)
        return success()
    }

    /**
     * 邮箱验证码验证
     * @return
     */
    @ApiOperation(value = "邮箱验证码验证", httpMethod = "POST", notes = "邮箱验证码验证")
    @PostMapping("/validationEmail")
    fun validationEmail(@RequestBody request: EmailRequest): Map<String, Any> {
        return messageService!!.validationEmail(request)
    }
}
